package pro.softzhang.algo.lc800;

import pro.softzhang.algo.flag.x.XClassic;

import java.util.List;

/**
 * https://leetcode.cn/problems/keys-and-rooms/solution/7xing-dfs-8xing-bfs-liang-chong-fang-fa-san-chong-/
 */
@XClassic
public class LC841_KeysAndRooms {
    public static void main(String[] args) {

    }

    static
    class Solution {
        private boolean[] visited;
        private int num;

        public boolean canVisitAllRooms(List<List<Integer>> rooms) {
            int n = rooms.size();
            visited = new boolean[n];
            dfs(rooms, 0);
            return n == num;
        }

        private void dfs(List<List<Integer>> rooms, int roomId) {
            visited[roomId] = true;
            num++;
            for(int i : rooms.get(roomId)) {
                if(!visited[i]) {
                    dfs(rooms, i);
                }
            }
        }
    }
}